package com.cn.longdistancebusstation.contacter;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.adapter.LinkmanAdapter;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Contacter;
import com.cn.longdistancebusstation.model.ListResult;
import com.cn.longdistancebusstation.view.PassengerListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 常用联系人列表
 * Created by mucheng on 2018/1/25.
 */

public class ContacterListActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
    private PassengerListView mPassengerListViewist;
    private LinkmanAdapter mAdapter;
    private ArrayList<Contacter> mContacterList =new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linkman);
        this.getTitleTextView().setText("联系人");
        this.showMenu1("",R.mipmap.ic_menu_add);
        initView();
    }

    private void initView() {
//        back=(ImageView)findViewById(R.id.back);
//        back.setOnClickListener(this);
//        add=(ImageView)findViewById(R.id.add_linkman);
//        add.setOnClickListener(this);
        mPassengerListViewist=(PassengerListView) findViewById(R.id.list_linkman);
        mPassengerListViewist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                Intent intent = new Intent(ContacterListActivity.this,ContacterDetailActivity.class);
                intent.putExtra("contacter", mContacterList.get(position));
                startActivity(intent);

            }
        });
    }

    private void getContacter() {
        mContacterList.clear();
        final SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
        String id=sharedPreferences.getString("id","");
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<ListResult<Contacter>> call=httpService.getCommonContacts(GlobalVariable.getUserID());
        showHud();
        call.enqueue(new Callback<ListResult<Contacter>>() {
            @Override
            public void onResponse(Call<ListResult<Contacter>> call, Response<ListResult<Contacter>> response) {
                hideHud();
                ListResult result=response.body();
                Gson gson=new Gson();
                String strbody=gson.toJson(result.getBody().getData());
                Log.d("乘车人员",strbody);
                Type type=new TypeToken<List<Contacter>>(){}.getType();
                mContacterList =gson.fromJson(strbody, type);
                mAdapter =new LinkmanAdapter(ContacterListActivity.this, mContacterList);
                mPassengerListViewist.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                if(mContacterList.size()==0){
                    initEmptyView(R.id.passengers_list);
                }
            }

            @Override
            public void onFailure(Call<ListResult<Contacter>> call, Throwable t) {
                hideHud();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
//            case R.id.back:
//                finish();
//                break;
//            case R.id.add_linkman:
//                add_linkman();
//                break;

        }

    }

    private void add_linkman() {
        Intent in=new Intent(ContacterListActivity.this, AddNewPassengersActivity.class);
        startActivity(in);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getContacter();
    }
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        MenuInflater menuInflater = new MenuInflater(ContacterListActivity.this);
//        getMenuInflater().inflate(R.menu.menu_add,menu);
//        return super.onCreateOptionsMenu(menu);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if(item.getItemId() == R.id.item_add){
//            add_linkman();
//            return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }

    @Override
    public void onMenu1Click() {
        super.onMenu1Click();
        add_linkman();
    }
}
